const request = require('../../utils/request')

Page({
  data: {
    switching: true
  },
  onLoad() {

    this.setData({
      switching: this.data.switching ? 'd5t' : 'd5v'
    })

    // let devName = '在客厅插着空调的插座';
    // if (devName.length * 14 >= 126) {
    //   devName = devName.substring(0, devName.length - 2) + '...'
    // }
    // this.setData({
    //   devName: devName
    // })

    let obj1 = {
      devType: 111,
      devName: '1在客厅插着空调的插座',
      devArea: '客厅'
    }
    let obj2 = {
      devType: 111,
      devName: '2在客厅插着空调的插座',
      devArea: '客厅'
    }
    let obj3 = {
      devType: 111,
      devName: '3在客厅插着空调的插座',
      devArea: '客厅'
    }
    let obj4 = {
      devType: 111,
      devName: '4在客厅插着空调的插座',
      devArea: '客厅'
    }
    let myDevs1 = [obj1, obj2]
    let myDevs2 = [obj3, obj4]
    let myDevsList = [myDevs1, myDevs2]
    myDevsList.forEach(item => {
      item.forEach(obj => {
        if (obj.devName.length * 14 >= 126) {
          obj.devName = obj.devName.substring(0, 7) + '...'
        }
      })
    })
    this.setData({
      myDevsList: myDevsList
    })

    let titleInfo = wx.getMenuButtonBoundingClientRect()
    this.editDoduleSize(titleInfo)
  },
  errorResult(errorInfo) {
    wx.showToast({
      title: errorInfo,
      icon: 'error',
      duration: 2000
    })
  },
  successResult(successInfo) {
    wx.showToast({
      title: successInfo,
      icon: 'success',
      duration: 2000
    })
  },

  editDoduleSize(titleInfo) {
    let _this = this
    let titleBottom = titleInfo.bottom
    wx.getSystemInfo({
      success(res) {
        console.log(res);
        let screenHeight = res.screenHeight
        let screenWidth = res.screenWidth
        console.log((screenWidth - 30) / 2);
        _this.setData({
          titleHeight: titleBottom,
          devHeight: screenHeight - titleBottom,
          devWidth: screenWidth
        })
      }
    })
  },

  devSwitching() {
    if (this.data.switching) {
      this.setData({
        switching: 'd5v'
      })
      this.data.switching = false
    } else {
      this.setData({
        switching: 'd5t'
      })
      this.data.switching = true
    }
    console.log(this.data.switching);
  }
})